//
//  XYTitleButton.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/11.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYTitleButton: UIButton {

    var didTitleButton:((titleButton:XYTitleButton)->())?
    //给Home控制器添加一个按钮的titleView
    func TitleButton(titleName:String="首页") -> XYTitleButton {
        self.setTitle(titleName, forState: UIControlState.Normal)
        self.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        self.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        self.setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Selected)
        
        self.addTarget(self, action: "didClickTitleButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.sizeToFit()
        
        return self
    }

    //给按钮中的控制布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.titleLabel!.x=0
        self.imageView!.x=CGRectGetMaxX(self.titleLabel?.frame ?? CGRectZero) + 5
    }
    
    //实现按钮的点击事件
    @objc private func didClickTitleButton( ){
        
        //如果实现了闭包就调用
        if let didButton = self.didTitleButton{
            didButton(titleButton: self)
        }
    }
}





